home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-04-27 | 26.0 KB | 1,173 lines | [TEXT/MPS ] |
- Program BattMonitor;
- {$R+}
- {$OV+}
- {$W+}
- {$SC+}
- {$D+}
- {
- Created: Wednesday, June 17, 1992 at 10:39 PM
- creator = 'Batø' BattMonitor
-
- vers who what
- 0.00 jcr6 file created
- 0.02 jcr6 fixed ScrollRect bug?
- 0.03 jcr6 inval rather than redraw..
- 0.04 jcr6 sample down /1 /2 /3 /4 for both arrays
- 0.05 jcr6 add DLOG 128 - about box
-
-
- jcr6 = J. Christian Russ, Analytical Vision, Inc.
- ALink:D3115
-
- Written June 17-18, 1992 at MacHack
- By J. Christian Russ
-
- }
-
- USES
- Memtypes,OSIntf,Menus,Dialogs,Desk,Quickdraw,Fonts,
- ToolUtils,Windows,Resources,power;
-
- CONST
- AppleMenu = 128;
- FileMenu = 129;
- EditMenu = 130;
- ViewMenu = 131;
-
- QueueSize = 1000; {1000 fast samples, 1000 slow samples}
-
- SecTicks = {60}6; {1/10 sec}
- MinTicks = {3600}60; {every 1 sec}
- svWidth = 240;
- MyAboutBox = 128;
- TYPE
- PwrState = PACKED RECORD {xx bytes}
- pvolts : integer; {need to sign extend for derivative}
- pbacklight : byte;
- pIdle : boolean;
- pDiskOn : boolean;
- pSndOn : boolean;
- pModemOn : boolean;
- pPortA : boolean;
- pPortB : boolean; {modem}
- pCharging : boolean;
- END; {PwrState}
-
- History = Array[0..QueueSize-1] of PwrState;
- VAR
- theerr:oserr;
- myhandle : handle;
-
- ProgState : (ForegroundP,BackgroundP);
-
- AppleMenuH,
- FileMenuH,
- EditMenuH,
- ViewMenuH : MenuHandle;
-
- finished : boolean;
- Updating : boolean;
- hascolor : boolean;
-
- mywindow : windowptr;
-
- watch : curshandle;
- hasWNE : boolean; { has waitnextevent }
-
- SecHistory, {history for the last xxx seconds}
- dSecHistory,
- MinHistory,
- dMinHistory : History;
-
- SecStart,SecEnd : integer; {queue indicies, SecEnd=location for next entry}
- MinStart,MinEnd : integer; {queue indicies, minEnd=location for next entry}
-
- lastMinCount,
- lastSecCount : longint; {ticks for last inclusion into history arrays}
-
- bmode : (seconds, minutes, dseconds,dminutes);
- sampledown : integer; {1,2,3, or 4}
- drawmod :integer; {counter, whether it is time to draw a new value}
-
- PROCEDURE MacsBug; INLINE $A9FF;
-
- FUNCTION GetVoltage : integer ; External;
-
- FUNCTION TrapAvailable(tNumber:Integer; tType:TrapType):Boolean;
- CONST UnimplementedTrapNumber = $A89F;
- BEGIN
- TrapAvailable:= ( NGetTrapAddress(tNumber,tType) <> GetTrapAddress(UnimplementedTrapNumber) )
- END;{TrapAvailable}
-
-
- FUNCTION WNEIsImplemented:boolean;
- CONST WNETrapNumber = $A860;
- VAR
- theWorld : SysEnvRec;
- Err : OsErr;
- BEGIN
- Err:=SysEnvirons(1,theWorld); {Maybe I should use gestalt???}
-
- if theWorld.machineType<0 then
- WNEIsImplemented:=false
- else
- WNEIsImplemented:=TrapAvailable(WNETrapNumber,ToolTrap)
- END;{WNEisImplemented}
-
- FUNCTION ShouldIDoColor : boolean;
- VAR myGDHandle : GDHandle;
- screendepth : integer;
- BEGIN
- {++++ check trapavailable for GetGDevice}
- HasColor:=false;
- if not TrapAvailable($AA32,tooltrap) then
- BEGIN
- ShouldIDoColor:=false;
- exit(ShouldIDoColor);
- END;
- myGDHandle:=GetGDevice;
- if myGDHandle=NIL then
- ShouldIDoColor:=false
- else
- BEGIN
- HasColor:=true;
- ScreenDepth:=mygdhandle^^.gdPmap^^.pixelSize;
- ShouldIDoColor:=ScreenDepth>=4;
- END;
- END;{ShouldIDoColor}
-
-
- FUNCTION myGetNextEvent(eventMask:Integer; var theEvent:EventRecord):boolean;
- BEGIN
- if hasWNE then
- myGetNextEvent:=WaitNextEvent(eventMask,theEvent,5{ticks},nil)
- else
- myGetNextEvent:=GetNextEvent(eventMask,theEvent)
- END;
-
-
- FUNCTION OptionKeyDown : Boolean;
- TYPE KeyPtrType=^KeyMap;
- VAR KeyPtr:KeyPtrType; keys:ARRAY[0..3] OF LongInt;
- BEGIN KeyPtr:=KeyPtrType(@keys); GetKeys(KeyPtr^);
- OptionKeyDown:=(BAND(keys[1],4))<>0;
- END;{OptionKeyDown}
-
- FUNCTION ShiftKeyDown : Boolean;
- TYPE KeyPtrType=^KeyMap;
- VAR KeyPtr:KeyPtrType; keys:ARRAY[0..3] OF LongInt;
- BEGIN KeyPtr:=KeyPtrType(@keys); GetKeys(KeyPtr^);
- ShiftKeyDown:=(BAND(keys[1],1))<>0;
- END;{ShiftKeyDown}
-
- FUNCTION CommandKeyDown : Boolean;
- TYPE KeyPtrType=^KeyMap;
- VAR KeyPtr:KeyPtrType; keys:ARRAY[0..3] OF LongInt;
- BEGIN KeyPtr:=KeyPtrType(@keys); GetKeys(KeyPtr^);
- CommandKeyDown:=(BAND(keys[1],$8000))<>0; {For MPW}
- END;{CommandKeyDown}
-
-
- PROCEDURE SetMenuItem (menuh:menuhandle; itemnum:integer; on:boolean);
- {Enable or disable menuh's itemnum}
- BEGIN
- if menuH=NIL then exit(setMenuItem);
- IF on
- THEN EnableItem(menuh,itemnum)
- ELSE DisableItem(menuh,itemnum);
- IF ItemNum=0 THEN DrawMenuBar;
- END;{SetMenuItem}
-
-
- PROCEDURE ShowWatch;
- BEGIN
- watch := GetCursor(WatchCursor);
- SetCursor(watch^^);
- END;{showWatch}
-
-
- PROCEDURE OutlineButton(theDialog: DialogPtr; itemNo, CornerRad: integer);
- { Draws a border around a button. 16 is the normal cornerRad for small buttons }
- VAR
- itemType: Integer;
- itemBox: Rect;
- itemHdl: Handle;
- tempPort: GrafPtr;
- BEGIN
- GetPort(tempPort);
- SetPort(theDialog);
- GetDItem(theDialog, itemNo, itemType, itemHdl, itemBox);
- PenSize(3, 3);
- InSetRect(itemBox, -4, -4);
- FrameRoundRect(itemBox, cornerRad, cornerRad);
- PenSize(1,1);
- SetPort(tempPort);
- END;
-
- PROCEDURE DimButton(TheDialog : DialogPtr; item : integer);
- VAR ItemType : integer;
- ItemBox : rect;
- ItemHdl : handle;
- BEGIN
- GetDItem(TheDialog,item,ItemType,ItemHdl,ItemBox);
- HiliteControl(pointer(ItemHdl),255); {grey the button}
- END; {DimButton}
-
- PROCEDURE unDimButton(TheDialog : DialogPtr; item : integer);
- VAR ItemType : integer;
- ItemBox : rect;
- ItemHdl : handle;
- BEGIN
- GetDItem(TheDialog,item,ItemType,ItemHdl,ItemBox);
- HiliteControl(pointer(ItemHdl),0); {enable the button}
- END; {unDimButton}
-
-
- PROCEDURE DoAbout; {About me!!}
- VAR item : integer;
- mylog : dialogptr;
- BEGIN
- mylog:=GetNewDialog(myaboutbox,NIL,pointer(-1));
- outlinebutton(mylog,1,16);
- REPEAT
- ModalDialog(nil,item);
- UNTIL item=1;
- disposeDialog(mylog);
- END;{DoAbout}
-
-
- PROCEDURE SysResume;
- BEGIN
- FlushEvents(EveryEvent, 0);
- ExitToShell;
- END;
-
-
- FUNCTION Uppercase(a : str255) : str255;
- VAR i : integer; s:str255;
- BEGIN
- s:=a;
- if ord(s[0])>0 then
- for i:=1 to ord(s[0]) do
- if s[i] in ['a'..'z'] then s[i]:=chr(ord(s[i])-32);
- Uppercase:=s;
- END; {uppercase}
-
-
- PROCEDURE ChkOnOffItem(MenuH : MenuHandle; item,fst,lst : integer);
- VAR i : integer;
- BEGIN
- for i:=fst to lst do
- if i=item then Checkitem(MenuH,i,true)
- else CheckItem(MenuH,i,false);
- END; {ChkOnOffItem}
-
-
- PROCEDURE UpdateMenus;
- VAR tport : grafptr;
- i : integer;
- showitems : boolean;
- BEGIN
- getport(tport); {inval just the buttons that changed}
- setport(mywindow);
- setport(tport);
-
- {FileMenuH}
- SetMenuItem(FileMenuH,12,true); {Quit}
-
- {EditMenuH}
-
- {ViewMenuH}
- {ChkOnOffItem(ViewMenuH,ord4(DBQueryMode)+1,1,10);}
-
- END; {UpdateMenus}
-
-
- PROCEDURE SetUpMenus;
- VAR
- Apple:str255;
- Item,FontID:integer;
- BEGIN
- AppleMenuH:=GetMenu(AppleMenu);
- AddResMenu(AppleMenuH, 'DRVR');
- InsertMenu(AppleMenuH, 0);
-
- FileMenuH := GetMenu(FileMenu);
- InsertMenu(FileMenuH, 0);
-
- EditMenuH := GetMenu(EditMenu);
- InsertMenu(EditMenuH, 0);
-
- ViewMenuH := GetMenu(ViewMenu);
- InsertMenu(ViewMenuH, 0);
-
- DrawMenuBar;
- END; {SetUpMenus}
-
- PROCEDURE ScrollVolts(draweverything : boolean); Forward;
-
- PROCEDURE DoMenuEvent (MenuChoice : LongInt);
- VAR
- MenuID,MenuItem,i,ignore:integer;
- name,astr,ItemName:str255;
- tport : grafptr;
- BEGIN
- MenuID := HiWord(MenuChoice);
- MenuItem := LoWord(MenuChoice);
-
- CASE MenuID OF
- AppleMenu:
- BEGIN
- IF MenuItem=1 THEN
- BEGIN
- DoAbout;
- END
- ELSE BEGIN
- GetItem(GetMHandle(AppleMenu),MenuItem,name);
- ignore:=OpenDeskAcc(name)
- END;
- END;
-
- FileMenu:
- BEGIN
- CASE MenuItem OF
- 1..11:;{Blah!!!}
- 12:Finished:=true; {QUIT!}
- END;{case}
- END;
-
- EditMenu:
- BEGIN
- GetItem(GetMHandle(EditMenu), MenuItem, ItemName);
- IF NOT SystemEdit(MenuItem - 1) THEN
- CASE MenuItem OF
- 1:; {UNDO}
-
- {2:-}
- 3:{DoCut};
- 4:{DoCopy};
- 5:{DoPaste};
- 6:{DoClear};
- END;{case}
- END;
-
- ViewMenu:
- BEGIN
- CASE MenuItem OF
- 1:{voltage}
- BEGIN
- if bmode=dseconds then bmode:=seconds;
- if bmode=dminutes then bmode:=minutes;
- END;
- 2:{current}
- BEGIN
- if bmode=seconds then bmode:=dseconds;
- if bmode=minutes then bmode:=dminutes;
- END;
- {3:-}
- 4..7:{1/10,2/10,3/10,4/10 second}
- BEGIN
- if bmode=minutes then bmode:=seconds;
- if bmode=dminutes then bmode:=dseconds;
- sampledown:=MenuItem-3;
- END;
- 8..11:
- BEGIN
- if bmode=seconds then bmode:=minutes;
- if bmode=dseconds then bmode:=dminutes;
- sampledown:=Menuitem-7;
- END;
- END;{case}
- drawmod:=0; {modulo 0..sampledown-1}
- {ScrollVolts(true);}
- getport(tport);
- setport(mywindow);
- invalrect(mywindow^.portrect);
- setport(tport);
- END;
- END; {case MenuNumber}
-
- HiliteMenu(0);
- UpdateMenus;
- END;{DoMenuEvent}
-
-
- PROCEDURE DoKeyDown(event:EventRecord);
- VAR
- Ch : char;
- BEGIN
- Ch := chr(BitAnd(Event.message, 255));
- IF BitAnd(Event.modifiers,CmdKey)=CmdKey THEN
- BEGIN
- UpdateMenus;
- DoMenuEvent(MenuKey(Ch));
- exit(DoKeyDown);
- END;
-
- CASE ORD(ch) of
- {Esc} 27: BEGIN
- END;
- {F1}
- {F2}
- {Tab} 9: BEGIN
- END;
- {Return} 13,
- {EnterKey} 3: BEGIN
- END;
- {LeftArrow} 28:;
- {RightArrow} 29:;
- {DownArrow} 31: BEGIN
- END;
- {UpArrow} 30: BEGIN
- END;
- {ascii} 32..255:
- BEGIN
- END;
- END; {case}
- END;{DoKeyDown}
-
-
- PROCEDURE DoActivate(event:EventRecord);
- VAR
- WhichWindow:WindowPtr;
- Activating:boolean;
- I,kind:integer;
- tport : grafptr;
- BEGIN
- getport(tport);
- WhichWindow:=WindowPtr(event.message);
- kind:=WindowPeek(WhichWindow)^.WindowKind;
- Activating:= odd(event.modifiers);
- IF whichwindow=mywindow THEN
- BEGIN
- IF Activating THEN
- BEGIN
- {draw things that need to be shown w/ activate}
- {this also needs to be done with a resume event}
- END
- ELSE {not activating}
- BEGIN
- {erase things that need to be erased}
- END;
- END; {if it's mywindow}
-
- IF (NOT activating) THEN
- BEGIN
- WhichWindow:=FrontWindow;
- kind:=WindowPeek(WhichWindow)^.WindowKind;
- {IF kind<0 THEN ConvertClipboard;} {DA has become active}
- END;
- END;{DoActivate}
-
-
- PROCEDURE ScrollVolts(draweverything : boolean);
- VAR tport : grafptr;
- x,fx : integer;
- i,j : integer;
- svstart,svend : integer;
- r : rect;
- rgh : rgnHandle;
- BEGIN
- drawmod:=drawmod+1;
- GetPort(tport);
- setport(mywindow);
- case sampledown of
- 1: if draweverything then
- BEGIN
- with r do
- BEGIN
- top:=0;
- bottom:=257;
- left:=0;
- right:=svwidth+1;
- END;
- eraserect(r);
-
- if bmode in [seconds,dseconds] then
- BEGIN
- svend:=secEnd;
- svStart:=secEnd-svWidth;
- END
- else
- BEGIN
- svend:=minEnd;
- svStart:=minEnd-svWidth;
- END;
- if svStart<0 then svStart:=svStart+queueSize;
- x:=svstart;
-
- for i:=1 to svwidth do
- BEGIN
- case bmode of
- seconds: fx:=BAND(SecHistory[x].pvolts,$FF);
- minutes: fx:=BAND(MinHistory[x].pvolts,$FF);
- dseconds:fx:=(dSecHistory[x].pvolts)*4 +128;
- dminutes:fx:=(dMinHistory[x].pvolts)*4 + 128;
- end;{case}
- x:=(x+1)mod queuesize;
- if i=1 then moveto(i,256-fx);
- lineto(i,256-fx);
- END;
- END
- else
- BEGIN {draw in new channel}
- with r do
- BEGIN
- top:=0;
- bottom:=257;
- left:=0;
- right:=svwidth-1;
- END;
- rgh:=NewRgn;
- RectRgn(rgh,r);
-
- ScrollRect(r,-1,0,rgh);
- DisposeRgn(rgh);
- with r do
- BEGIN
- top:=0;
- bottom:=257;
- left:=right-1;
- right:=svwidth+1;
- END;
- eraserect(r);
- if bmode in [seconds,dseconds] then
- BEGIN
- svend:=secEnd;
- svStart:=secEnd-svWidth;
- END
- else
- BEGIN
- svend:=minEnd;
- svStart:=minEnd-svWidth;
- END;
- if svStart<0 then svStart:=svStart+queueSize;
- x:=svstart;
- for i:=1 to svwidth do
- BEGIN
- case bmode of
- seconds: fx:=BAND(SecHistory[x].pvolts,$FF);
- minutes: fx:=BAND(MinHistory[x].pvolts,$FF);
- dseconds:fx:=(dSecHistory[x].pvolts)*8 +128;
- dminutes:fx:=(dMinHistory[x].pvolts)*8 + 128;
- end;{case}
- x:=(x+1)mod queuesize;
- {draw only last line segment}
- if i=svwidth-3 then moveto(i,256-fx);
- if i>=svwidth-2 then lineto(i,256-fx);
- END;
- END;{draw only 1 event}
- 2,3,4 :if draweverything then {do it with sampledown}
- BEGIN
- drawmod:=0;
- with r do
- BEGIN
- top:=0;
- bottom:=257;
- left:=0;
- right:=svwidth+1;
- END;
- eraserect(r);
-
- if bmode in [seconds,dseconds] then
- BEGIN
- svend:=secEnd;
- svStart:=secEnd-svWidth*sampledown;
- END
- else
- BEGIN
- svend:=minEnd;
- svStart:=minEnd-svWidth*sampledown;
- END;
- if svStart<0 then svStart:=svStart+queueSize;
- x:=svstart;
-
- for i:=1 to svwidth do
- BEGIN
- fx:=0;
- for j:=1 to sampledown do
- BEGIN
- case bmode of
- seconds: fx:=fx+BAND(SecHistory[x].pvolts,$FF);
- minutes: fx:=fx+BAND(MinHistory[x].pvolts,$FF);
- dseconds:fx:=fx+(dSecHistory[x].pvolts);
- dminutes:fx:=fx+(dMinHistory[x].pvolts);
- end;{case}
- x:=(x+1)mod queuesize;
- END;{for}
- case sampledown of
- 2:fx:=fx*2; {fx:=fx*4 div 2}
- 3:fx:=(fx*4) div 3;
- 4:; {fx:=fx*4 div 4}
- end;{case}
- if bmode in [dseconds,dminutes] then fx:=fx*2+128
- else fx:=fx div 4; {back to normal voltage scale}
- if i=1 then moveto(i,256-fx);
- lineto(i,256-fx);
- END;
- END
- else
- BEGIN {draw in new channel}
- if drawmod=sampledown then drawmod:=0
- else
- BEGIN
- setport(tport);
- exit(ScrollVolts);
- END;
- with r do
- BEGIN
- top:=0;
- bottom:=257;
- left:=0;
- right:=svwidth;
- END;
-
- rgh:=NewRgn;
- RectRgn(rgh,r);
-
- ScrollRect(r,-1,0,rgh);
- DisposeRgn(rgh);
- with r do
- BEGIN
- top:=0;
- bottom:=257;
- left:=right-1;
- right:=svwidth+1;
- END;
- eraserect(r);
- if bmode in [seconds,dseconds] then
- BEGIN
- svend:=secEnd;
- svStart:=secEnd-svWidth*sampledown;
- END
- else
- BEGIN
- svend:=minEnd;
- svStart:=minEnd-svWidth*sampledown;
- END;
- if svStart<0 then svStart:=svStart+queueSize;
- x:=svstart;
- for i:=1 to svwidth do
- BEGIN
- fx:=0;
- for j:=1 to sampledown do
- BEGIN
- case bmode of
- seconds: fx:=fx+BAND(SecHistory[x].pvolts,$FF);
- minutes: fx:=fx+BAND(MinHistory[x].pvolts,$FF);
- dseconds:fx:=fx+(dSecHistory[x].pvolts);
- dminutes:fx:=fx+(dMinHistory[x].pvolts);
- end;{case}
- x:=(x+1)mod queuesize;
- END;{for}
- case sampledown of
- 2:fx:=fx*2; {fx:=fx*4 div 2}
- 3:fx:=(fx*4) div 3;
- 4:; {fx:=fx*4 div 4}
- end;{case}
- if bmode in [dseconds,dminutes] then fx:=fx*2+128
- else fx:=fx div 4; {back to normal voltage scale}
- if i=svwidth-1 then moveto(i,256-fx);
- if i>=svwidth then lineto(i,256-fx);
- END;
- END;{draw only 1 event}
-
- end; {case sampledown}
- Setport(tport);
- END; {ScrollVolts}
-
-
- PROCEDURE DoUpdate(event:EventRecord);
- VAR
- WhichWindow:WindowPtr;
- kind:integer;
- info : fontinfo;
- i:integer;
- tport : grafptr;
- r,lrect,r1:rect;
- pat:patHandle;
- dummyhdl : pichandle;
- astr, bstr:str255;
- sw : integer;
- hposn, vposn : integer;
- ww : integer;
-
-
- PROCEDURE DrawStringRight(s : str31; fw : integer);
- VAR k : integer;
- cp : boolean;
- BEGIN
- k:=StringWidth(s);
- cp:=k>fw;
- if cp then
- BEGIN
- TextFace([condense]);
- k:=StringWidth(s);
- END;
- Moveto(hposn+fw-k,vposn);
- drawstring(s);
- if cp then TextFace([]);
- END;{DrawStringRight}
-
- PROCEDURE DrawStringLeft(s : str31; fw : integer);
- VAR k : integer;
- cp : boolean;
- BEGIN
- k:=StringWidth(s);
- cp:=k>fw;
- if cp then
- BEGIN
- TextFace([condense]);
- k:=StringWidth(s);
- END;
- Moveto(hposn,vposn);
- drawstring(s);
- if cp then TextFace([]);
- END;{DrawStringLeft}
-
- PROCEDURE DrawStringCenter(s : str31; fw : integer);
- VAR k : integer;
- cp : boolean;
- BEGIN
- k:=StringWidth(s);
- cp:=k>fw;
- if cp then
- BEGIN
- TextFace([condense]);
- k:=StringWidth(s);
- END;
- Moveto(hposn+(fw div 2) - (k div 2),vposn);
- drawstring(s);
- if cp then TextFace([]);
- END;{DrawStringCenter}
-
- BEGIN
- pat:=getPattern(132); { the fill pattern }
- WhichWindow := WindowPtr(event.message);
- kind:=WindowPeek(WhichWindow)^.WindowKind;
-
- showwatch;
- Updating:=true;
- getport(tport);
- setport(whichwindow);
- BeginUpdate(WhichWindow);
-
- if whichwindow=mywindow then
- BEGIN
- {draw that which needs be drawn}
- ScrollVolts(true);
- {draw any controls that need to be shown - Minutes/Seconds
- Volts, ∂Volts}
- with r do
- BEGIN
- top:=0;
- bottom:=260;
- left:=svwidth+2;
- right:=left+80;
- END;
- eraserect(r);
-
- {Draw Labels}
- TextFont(1);
- TextSize(9);
- TextFace([]);
- if bmode in [seconds,minutes] then
- for i:=1 to 5 do {5.5,6,6.5,7,7.5 volts}
- BEGIN
- hposn:=svwidth+2;
- vposn:=(5-i)*50+18;
- moveto(hposn,vposn);
- lineto(hposn+10,vposn);
- hposn:=hposn+12;
- case i of
- 1:astr:='5.5 volts';
- 2:astr:='6.0 volts';
- 3:astr:='6.5 volts';
- 4:astr:='7.0 volts';
- 5:astr:='7.5 volts';
- end; {case}
- DrawStringLeft(astr,40);
- END{for}
- else
- for i:=-4 to 4 do {ticks on derivative scale}
- BEGIN
- hposn:=svwidth+2;
- vposn:=(5-i)*25+3;
- moveto(hposn,vposn);
- lineto(hposn+10,vposn);
- hposn:=hposn+12;
-
- END;{for}
- TextFont(0);
- TextSize(12);
- textFace([]);
-
- END;{update mywindow}
-
- EndUpdate(WhichWindow);
- setport(tport);
- Updating:=false;
- initcursor;
- END;{DoUpdate}
-
-
- PROCEDURE DoDrag(WhichWindow:WindowPtr; loc:point);
- VAR
- WinRect,DragBounds:rect;
- i, imagenum : integer;
- BEGIN
- DragBounds:=ScreenBits.bounds;
- DragWindow(WhichWindow,loc,DragBounds);
- END;{DoDrag}
-
-
- PROCEDURE DoMouseDown(event:EventRecord);
- VAR
- WhichWindow:WindowPtr;
- ThePart,ignore:integer;
- byebye : boolean;
- tport : grafptr;
- loc : point;
- invbox : boolean;
- invrect,r,rtri : rect;
- invbutton,oldbutton : integer;
- devent: eventrecord;
- i:integer;
- mycell:point;
- datalen : integer;
- caninvbutton : boolean;
- myhandle1,myhandle2 : pichandle;
- BEGIN
- caninvbutton:=not ShouldIDoColor;
- ThePart:=FindWindow(event.where,WhichWindow);
- CASE ThePart OF
- InDesk:;
- InMenuBar:BEGIN
- UpdateMenus;
- DoMenuEvent(MenuSelect(event.where));
- END;
- InSysWindow:SystemClick(Event,WhichWindow);
- InContent:
- BEGIN
- IF (WhichWindow=mywindow) THEN
- BEGIN
- {Figure out what to do...}
- getport(tport);
- setport(mywindow);
- loc:=event.where;
- globaltolocal(loc);
-
- setport(tport);
-
- Exit(DoMouseDown);
- END;
- IF WhichWindow<>FrontWindow THEN
- BEGIN
- SelectWindow(WhichWindow);
- END;
- END;{incontent}
- InDrag:DoDrag(WhichWindow,event.where);
- InGrow:{DoGrow(WhichWindow,event)};
- InGoAway:
- BEGIN
- byebye:=TrackGoAway(WhichWindow,event.where);
- END;
- InZoomIn,InZoomOut:{DoZoom(WhichWindow,event)};
- END;
- END; {DoMouseDown}
-
-
- PROCEDURE DoDiskInsert(event:EventRecord);
- VAR p : point;
- intjunk : integer;
-
- BEGIN {DoDiskInsert}
- IF (HiWord(event.message) <> NoErr) THEN
- BEGIN
- DiLoad;
- SetPt(p, 100, 80);
- intjunk := DiBadMount(p, event.message);
- DiUnload;
- END;
- END;{DoDiskInsert}
-
-
- FUNCTION HandleEvents : boolean;
- CONST
- osevent = app4Evt;
- suspendresumemessage = 1;
- resumemask = 1;
- mouemovedmessage = $FA;
- VAR
- Event : EventRecord;
- whichwindow : windowptr;
- b : boolean;
- thePart : integer;
- myMenu : menuHandle;
-
- BEGIN
- b:=myGetNextEvent(everyEvent,Event); {should use WaitNextEvent if in background}
- if b then {normal event, background update/activate, AEvent?}
- with Event do
- case what of
- KeyDown,AutoKey:DoKeyDown(event);
- MouseDown:DoMouseDown(event);
- ActivateEvt:DoActivate(event);
- DiskEvt:DoDiskInsert(event);
- UpdateEvt:DoUpdate(event);
- OSevent:case BSR(message,24) of
- mousemovedmessage:;
- suspendresumemessage: if band(message,resumemask)<>0 then
- BEGIN{resume event}
- progstate:=foregroundP;
- message:=ord4(frontwindow);
- modifiers:=ord(true);{activate}
- DoActivate(event); {multifinder aware}
- END
- else
- BEGIN
- progstate:=backgroundP;
- message:=ord4(frontwindow);
- modifiers:=ord(false);{deactivate}
- DoActivate(event); {multifinder aware}
- {convert clipboard, if needed}
- END;
- END; {case message»24}
- END{case what, with}
- else handleevents:=false;
- END;{handleEvents}
-
-
- PROCEDURE DoStartup;
- VAR i:integer;
- BEGIN
- Finished:=false;
- ProgState:=ForegroundP;
- updating:=false;
- hasWNE:=WNEIsImplemented;
-
- SecStart:=-1; {flag for first time}
- SecEnd:=0; {queue indicies, SecEnd=location for next entry}
- MinStart:=-1; {flag for first time}
- MinEnd:=0; {queue indicies, minEnd=location for next entry}
-
- lastMinCount:=0; {flag for first time}
- lastSecCount:=0; {flag for first time through}
-
- bmode:=seconds;
- for i:=0 to queueSize-1 do
- BEGIN
- SecHistory[i].pvolts:=0;
- dSecHistory[i].pvolts:=0;
- SecHistory[i].pvolts:=0;
- dSecHistory[i].pvolts:=0;
- END;
- sampledown:=1; {1,2,3, or 4}
- END; {DoStartup}
-
-
- PROCEDURE MakeWindow;
- VAR cell : point;
- databounds, ibox : rect;
- BEGIN
- if ShouldIDoColor then ; {set up the HasColor flag}
-
- if not HasColor then
- mywindow:=GetNewWindow(1000,NIL,pointer(-1) )
- else myWindow:=WindowPtr(GetNewCWindow(1000,NIL,pointer(-1)));
-
- UpdateMenus; {cannot update until the window is drawn}
-
- END;{makewindow}
-
-
- FUNCTION SampleVolts : boolean;
- VAR now : longint;
- IsIdle : boolean;
- Volts : integer;
- ModemOn : boolean;
- isCharging : boolean;
-
- Function Sample : boolean;
- VAR theErr : oserr;
- status : byte;
- power : byte;
- BEGIN
- isIdle:=GetCPUSpeed=1; {1MHz is idled}
- theErr:=ModemStatus(status);
- if theerr<>noerr then
- BEGIN
- sample:=false;
- exit(sample);
- END;
-
- ModemOn:=BAND(status,$01)=$01;
-
- power:=$AA;
- status:=$55;
-
- theErr:=BatteryStatus(status,power);
-
- if theErr<>noerr then
- BEGIN
- sample:=false;
- exit(sample);
- END;
-
- Volts:=GetVoltage; { (512+power) div 100 = volts}
- isCharging:=(BAND(status,$01{connected})=$01);
- sample:=true;
- END;{sample}
-
- BEGIN
- now:=TickCount;
- if lastSecCount=0 then
- BEGIN {initialize everything - 1 element in both queues, etc}
- if not Sample then
- BEGIN
- SampleVolts:=false;
- exit(SampleVolts);
- END;
- SecStart:=0;
- SecEnd:=0;
-
- with SecHistory[SecEnd] do
- BEGIN
- pIdle:=isIdle;
- pvolts:=volts;
- {pbacklight:= help barry}
- pDiskOn:=true; {we'll assume on, later we will notice spikes and
- adjust this flag}
- pSndOn:=false; {usually off}
- pModemOn:=ModemOn;
- pPortA:=false; {should really ask about this}
- pPortB:=false; {should really ask about this}
- pCharging:=isCharging;
- END;{with}
-
- dSecHistory[SecEnd]:=SecHistory[SecEnd];
- dSecHistory[SecEnd].pvolts:=0; {no change}
-
- SecEnd:=(SecEnd+1) mod QueueSize;
-
- SampleVolts:=true;
- lastSecCount:=now;
- if true then
- BEGIN {add to per minute queue, may be forced to toss stale data}
- MinStart:=0;
- MinEnd:=0;
-
- with MinHistory[MinEnd] do
- BEGIN
- pIdle:=isIdle;
- pvolts:=volts;
- {pbacklight:= help barry}
- pDiskOn:=true; {we'll assume on, later we will notice spikes and
- adjust this flag}
- pSndOn:=false; {usually off}
- pModemOn:=ModemOn;
- pPortA:=false; {should really ask about this}
- pPortB:=false; {should really ask about this}
- pCharging:=isCharging;
- END;{with}
-
- dMinHistory[MinEnd]:=MinHistory[MinEnd];
- dMinHistory[MinEnd].pvolts:=0; {no change}
-
- MinEnd:=(MinEnd+1) mod QueueSize;
- END;{add to minute history}
- END{first time through}
- else if now>=lastSecCount+SecTicks {60 ticks = 1 second} then
- BEGIN {add to second queue, may be forced to toss old data}
- if not Sample then
- BEGIN
- SampleVolts:=false;
- exit(SampleVolts);
- END;
- if SecStart=SecEnd then
- SecStart:=(SecStart+1) mod QueueSize;
-
- with SecHistory[SecEnd] do
- BEGIN
- pIdle:=isIdle;
- pvolts:=volts;
- {pbacklight:= help barry}
- pDiskOn:=true; {we'll assume on, later we will notice spikes and
- adjust this flag}
- pSndOn:=false; {usually off}
- pModemOn:=ModemOn;
- pPortA:=false; {should really ask about this}
- pPortB:=false; {should really ask about this}
- pCharging:=isCharging;
- END;{with}
-
- dSecHistory[SecEnd]:=SecHistory[SecEnd];
- if SecEnd=0 then
- dSecHistory[SecEnd].pvolts:=SecHistory[0].pvolts-SecHistory[QueueSize-1].pvolts
- else
- dSecHistory[SecEnd].pvolts:=SecHistory[SecEnd].pvolts-SecHistory[SecEnd-1].pvolts;
-
- SecEnd:=(SecEnd+1) mod QueueSize;
-
- SampleVolts:=(bmode=seconds) or (bmode=dseconds);
- lastSecCount:=now;
- if lastSecCount>=lastMinCount+MinTicks then
- BEGIN {add to per minute queue, may be forced to toss stale data}
- if MinStart=MinEnd then
- MinStart:=(MinStart+1) mod QueueSize;
- lastmincount:=now;
- with MinHistory[MinEnd] do
- BEGIN
- pIdle:=isIdle;
- pvolts:=volts;
- {pbacklight:= help barry}
- pDiskOn:=true; {we'll assume on, later we will notice spikes and
- adjust this flag}
- pSndOn:=false; {usually off}
- pModemOn:=ModemOn;
- pPortA:=false; {should really ask about this}
- pPortB:=false; {should really ask about this}
- pCharging:=isCharging;
- END;{with}
-
- dMinHistory[MinEnd]:=MinHistory[MinEnd];
- if SecEnd=0 then
- dMinHistory[MinEnd].pvolts:=MinHistory[0].pvolts-MinHistory[QueueSize-1].pvolts
- else
- dMinHistory[MinEnd].pvolts:=MinHistory[MinEnd].pvolts-MinHistory[MinEnd-1].pvolts;
-
- MinEnd:=(MinEnd+1) mod QueueSize;
-
- SampleVolts:=true;
- END;{add to minute history}
- END{add to second queue}
- else SampleVolts:=false;
- END; {SampleVolts}
-
-
- BEGIN {Main Prog}
- SetApplLimit(ptr(LongInt(GetApplLimit)-16000));
- MaxApplZone;
-
- InitGraf(@ThePort);
- InitFonts;
- Flushevents(everyevent,0);
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(@SysResume);
- InitCursor;
-
- SetUpMenus;
- DoStartup;
- MakeWindow;
-
- {Macsbug;}
-
- REPEAT {main event Loop}
- if not hasWNE then
- SystemTask; {actions defined for desk accessories}
- if HandleEvents then ;
- if SampleVolts then ScrollVolts(false);
- UNTIL finished;
- END. {BattMonitor}